package cn.exrick.xboot.modules.content.serviceimpl;

import cn.exrick.xboot.common.utils.SecurityUtil;
import cn.exrick.xboot.modules.base.dao.DepartmentHeaderDao;
import cn.exrick.xboot.modules.base.entity.DepartmentHeader;
import cn.exrick.xboot.modules.content.dao.HontDepartmentDao;
import cn.exrick.xboot.modules.content.dao.HontDepartmentHeaderDao;
import cn.exrick.xboot.modules.content.entity.HontDepartment;
import cn.exrick.xboot.modules.content.entity.HontDepartmentHeader;
import cn.exrick.xboot.modules.content.service.HontDepartmentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.List;


/**
 * 测试接口实现
 * @author Exrick
 */
@Slf4j
@Service
@Transactional
public class HontDepartmentServiceImpl implements HontDepartmentService {

    @Autowired
    private HontDepartmentDao departmentDao;

    @Autowired
    private SecurityUtil securityUtil;
    @Autowired
    private HontDepartmentHeaderDao departmentHeaderDao;
    @Override
    public HontDepartmentDao getRepository() {
        return departmentDao;
    }

    @Override
    public List<HontDepartment> findByParentIdOrderBySortOrder(String parentId, Boolean openDataFilter) {

        // 数据权限
        List<String> depIds = securityUtil.getHontDeparmentIds();
        if(depIds!=null&&depIds.size()>0&&openDataFilter){
            return departmentDao.findByParentIdAndIdInOrderBySortOrder(parentId, depIds);
        }
        return departmentDao.findByParentIdOrderBySortOrder(parentId);
    }

    @Override
    public List<HontDepartment> findByParentIdAndStatusOrderBySortOrder(String parentId, Integer status) {

        return departmentDao.findByParentIdAndStatusOrderBySortOrder(parentId, status);
    }

    @Override
    public List<HontDepartment> findByTitleLikeOrderBySortOrder(String title, Boolean openDataFilter) {

        // 数据权限
        List<String> depIds = securityUtil.getHontDeparmentIds();
        if(depIds!=null&&depIds.size()>0&&openDataFilter){
            return departmentDao.findByTitleLikeAndIdInOrderBySortOrder(title, depIds);
        }
        return departmentDao.findByTitleLikeOrderBySortOrder(title);
    }

    @Override
    public List<HontDepartment> findByTitleLikeOrder(String title) {
        return departmentDao.findByTitleLikeOrderBySortOrder(title);
    }

    @Override
    public List<HontDepartment> departmentTree() {
        List<HontDepartment> list = findByParentIdOrderBySortOrder("0", false);

        for(int i=0;i<list.size();i++){
            List<HontDepartment> departments = cheldrenDepartment(list.get(i).getId());
            if(departments.size()>0){
                list.get(i).setChildrenDepartment(departments);
            }
            List<String> listMain=new ArrayList<String>();
            List<HontDepartmentHeader> byDepartmentIdAndType = departmentHeaderDao.findByHontDepartmentIdAndType(list.get(i).getId(), 0);
            for(int j=0;j<byDepartmentIdAndType.size();j++){
                listMain.add(byDepartmentIdAndType.get(j).getHontUserId());
            }
            list.get(i).setMainHeader(listMain);
            List<HontDepartmentHeader> viceDepartmentIdAndType = departmentHeaderDao.findByHontDepartmentIdAndType(list.get(i).getId(), 1);
            List<String> viceMain=new ArrayList<String>();
            for(int j=0;j<viceDepartmentIdAndType.size();j++){
                viceMain.add(viceDepartmentIdAndType.get(j).getHontUserId());
            }
            list.get(i).setViceHeader(viceMain);
        }

        return list;
    }

    @Override
    public List<HontDepartment> findByParentIdOrderBySortOrder(String parentId) {
        return departmentDao.findByParentIdOrderBySortOrder(parentId);
    }

    public List<HontDepartment> cheldrenDepartment(String id){
        List<HontDepartment> list = findByParentIdOrderBySortOrder(id, false);
        for(int i=0;i<list.size();i++){
            List<HontDepartment> departments = cheldrenDepartment(list.get(i).getId());
            if(departments.size()>0){
                list.get(i).setChildrenDepartment(departments);
            }
            List<String> listMain=new ArrayList<String>();
            List<HontDepartmentHeader> byDepartmentIdAndType = departmentHeaderDao.findByHontDepartmentIdAndType(list.get(i).getId(), 0);
            for(int j=0;j<byDepartmentIdAndType.size();j++){
                listMain.add(byDepartmentIdAndType.get(j).getHontUserId());
            }
            list.get(i).setMainHeader(listMain);
            List<HontDepartmentHeader> viceDepartmentIdAndType = departmentHeaderDao.findByHontDepartmentIdAndType(list.get(i).getId(), 1);
            List<String> viceMain=new ArrayList<String>();
            for(int j=0;j<viceDepartmentIdAndType.size();j++){
                viceMain.add(viceDepartmentIdAndType.get(j).getHontUserId());
            }
            list.get(i).setViceHeader(viceMain);
        }
        return list;
    }

    @Override
    public List<HontDepartment> getAllTeamList() {
        return departmentDao.findAll(new Specification<HontDepartment>() {
            @Override
            public Predicate toPredicate(Root<HontDepartment> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> list = new ArrayList<Predicate>();
                Path<Boolean> indexName = root.get("isParent");
                list.add(criteriaBuilder.equal(indexName,0));
                Predicate[] arr = new Predicate[list.size()];
                criteriaQuery.where(list.toArray(arr));
                return null;
            }
        });
    }

    @Override
    public HontDepartment findByTitle(String departmentTitles) {
        return departmentDao.findByTitle(departmentTitles);
    }
}